<?php
/**
 * ============================================================================
 * Copyright (c) 2015-2018 贵州大师兄信息技术有限公司 All rights reserved.
 * siteַ: http://www.dsxcms.com
 * ============================================================================
 * @author:     David Song<songdewei@163.com>
 * @version:    v1.0.0
 * ---------------------------------------------
 * Date: 2018/9/11
 * Time: 下午6:43
 */

namespace App\Library;


class SysMessager
{
    const MSGTYPE_SUCCESS    = 'success';
    const MSGTYPE_INFOMATION = 'infomation';
    const MSGTYPE_ERROR      = 'error';
    const MSGTYPE_WARNING    = 'warning';

    protected $data = [
        'redirectTo' => '',
        'message' => '',
        'type' => 'success',
        'links' => [],
        'tips' => '',
        'autoredirect' => false,
        'timeout' => 5,
    ];
    protected $view = 'common.message';

    public function __construct()
    {
        $this->redirectTo(back()->getTargetUrl());
        $this->message(trans('sysmessage.info save success'));
    }

    /**
     * @param $redirectTo
     * @return $this
     */
    public function redirectTo($redirectTo)
    {
        $this->data['redirectTo'] = $redirectTo;
        return $this;
    }

    /**
     * @param $message
     * @return $this
     */
    public function message($message) {
        $this->data['message'] = $message;
        return $this;
    }

    /**
     * @param $type
     * @return $this
     */
    public function type($type) {
        $this->data['type'] = $type;
        return $this;
    }

    /**
     * @param array $links
     * @return $this
     */
    public function links(array $links)
    {
        $this->data['links'] = $links;
        return $this;
    }

    /**
     * @param $tips
     * @return $this
     */
    public function tips($tips)
    {
        $this->data['tips'] = $tips;
        return $this;
    }


    /**
     * @param bool $autoredirect
     * @return $this
     */
    public function autoredirect($autoredirect = true)
    {
        $this->data['autoredirect'] = $autoredirect;
        return $this;
    }


    /**
     * @param $timeout
     * @return $this
     */
    public function timeout($timeout)
    {
        $this->data['timeout'] = $timeout;
        return $this;
    }

    public function appends($data)
    {
        if(is_array($data)) {
            foreach ($data as $key => $value)
            {
                $this->data[$key] = $value;
            }
        }

        return $this;
    }

    /**
     * @param $view
     * @return $this
     */
    public function view($view)
    {
        $this->view = $view;
        return $this;
    }

    public function success()
    {
        $this->data['type'] = self::MSGTYPE_SUCCESS;
        return $this;
    }

    public function error()
    {
        $this->data['type'] = self::MSGTYPE_ERROR;
        return $this;
    }

    public function warning()
    {
        $this->data['type'] = self::MSGTYPE_WARNING;
        return $this;
    }

    public function infomation()
    {
        $this->data['type'] = self::MSGTYPE_INFOMATION;
        return $this;
    }

    /**
     * @param string|null $view
     * @param array $data
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function render($view = null, $data = []) {
        if ($view) $this->view = $view;
        if (!empty($data))
        {
            $this->appends($data);
        }

        if (!$this->data['tips']) {
            if ($this->data['autoredirect']) {
                $this->tips(trans('sysmessage.auto_redirect_tips', ['timeout'=>$this->data['timeout']]));
            } else {
                $this->tips(trans('sysmessage.default_tips'));
            }
        }
        return view($this->view, $this->data);
    }

    /**
     * @param $message
     * @param null $redirectTo
     * @param array $links
     * @param null $tips
     * @param bool $autoredirect
     * @param int $timeout
     * @param null $view
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public static function showSuccess($message, $redirectTo = null, $links = [], $tips = null, $autoredirect = false, $timeout = 5, $view = null)
    {
        $messager = new static();
        return $messager->showMessage(self::MSGTYPE_SUCCESS, $message, $redirectTo, $links, $tips, $autoredirect, $timeout);
    }

    /**
     * @param $message
     * @param null $redirectTo
     * @param array $links
     * @param null $tips
     * @param bool $autoredirect
     * @param int $timeout
     * @param null $view
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public static function showInfomation($message, $redirectTo = null, $links = [], $tips = null, $autoredirect = false, $timeout = 5, $view = null)
    {
        $messager = new static();
        return $messager->showMessage(self::MSGTYPE_INFOMATION, $message, $redirectTo, $links, $tips, $autoredirect, $timeout);
    }

    /**
     * @param $message
     * @param null $redirectTo
     * @param array $links
     * @param null $tips
     * @param bool $autoredirect
     * @param int $timeout
     * @param null $view
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public static function showError($message, $redirectTo = null, $links = [], $tips = null, $autoredirect = false, $timeout = 5, $view = null)
    {
        $messager = new static();
        return $messager->showMessage(self::MSGTYPE_ERROR, $message, $redirectTo, $links, $tips, $autoredirect, $timeout);
    }

    /**
     * @param $message
     * @param null $redirectTo
     * @param array $links
     * @param null $tips
     * @param bool $autoredirect
     * @param int $timeout
     * @param null $view
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function showWarning($message, $redirectTo = null, $links = [], $tips = null, $autoredirect = false, $timeout = 5, $view = null)
    {
        $messager = new static();
        return $messager->showMessage(self::MSGTYPE_WARNING, $message, $redirectTo, $links, $tips, $autoredirect, $timeout);
    }

    /**
     * @param $type
     * @param $message
     * @param null $redirectTo
     * @param array $links
     * @param null $tips
     * @param bool $autoredirect
     * @param int $timeout
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    private function showMessage($type, $message, $redirectTo = null, $links = [], $tips = null, $autoredirect = false, $timeout = 5, $view = null)
    {
        if ($type) {
            $this->type($type);
        }

        if ($message) {
            $this->message($message);
        }

        if ($redirectTo)
        {
            $this->redirectTo($redirectTo);
        }

        if ($links)
        {
            $this->links($links);
        }

        if ($tips)
        {
            $this->tips($tips);
        }

        if ($autoredirect)
        {
            $this->autoredirect($autoredirect);
        }

        if ($timeout)
        {
            $this->timeout($timeout);
        }

        if ($view)
        {
            $this->view($view);
        }

        return $this->render();
    }
}
